﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

/// <summary>
/// Summary description for Invetory_session
/// </summary>
public class Invetory_session
{
    private static SqlConnection sqlConnection;

    private DataTable objDataTable;
    public static SqlDataAdapter sqlAdapter;
    public static SqlCommand sqlCommand; 
	public Invetory_session()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public static void Insert_Invetory(Invetory invetory)
    {

        sqlConnection = DataProvider.CreateConnection();

        //================================== Thỏa mãn các điều kiện và thêm vào ============================//
        DataTable datatable = new DataTable();
        sqlAdapter = new SqlDataAdapter("select * from Invetory", sqlConnection);
        sqlAdapter.Fill(datatable);

        DataRow datarow = datatable.NewRow();
        datarow[0] = invetory.Inv_ID;
        datarow[1] = invetory.Inv_CreateDate;
        datarow[2] = invetory.Inv_TotalPrice;
        datarow[3] = invetory.Inv_TotalQuantity;
        datarow[4] = invetory.Int_Status;
        datarow[5] = invetory.User_ID;
        datarow[6] = invetory.Inv_UserRevice;
        datarow[7] = invetory.Inv_UserRevicePhone;
        datarow[8] = invetory.Inv_UserReviceAddress;
        datarow[9] = invetory.Inv_PayType;
        datarow[10] = invetory.Inv_DateRevice;
        datarow[11] = invetory.Local_ID;
        datarow[12] = invetory.Inv_Note;
        datarow[13] = invetory.Inv_UserReviceEmail;


        datatable.Rows.Add(datarow);

        sqlCommand = new SqlCommand();
        sqlCommand.Connection = sqlConnection;
        sqlCommand.CommandType = CommandType.Text;
        sqlCommand.CommandText = "INSERT INTO Invetory VALUES(@Inv_ID,@Inv_CreateDate,@Inv_TotalPrice,@Inv_TotalQuantity,@Int_Status,@User_ID,@Inv_UserRevice,@Inv_UserRevicePhone,@Inv_UserReviceAddress,@Inv_PayType,@Inv_DateRevice,@Local_ID,@Inv_Note,@Inv_UserReviceEmail)";
        sqlCommand.Parameters.Add("@Inv_ID", SqlDbType.VarChar, 30, "Inv_ID");
        sqlCommand.Parameters.Add("@Inv_CreateDate", SqlDbType.DateTime, 50, "Inv_CreateDate");
        sqlCommand.Parameters.Add("@Inv_TotalPrice", SqlDbType.Float, 10, "Inv_TotalPrice");
        sqlCommand.Parameters.Add("@Inv_TotalQuantity", SqlDbType.Int, 10, "Inv_TotalQuantity");
        sqlCommand.Parameters.Add("@Int_Status", SqlDbType.Int, 10, "Int_Status");
        sqlCommand.Parameters.Add("@User_ID", SqlDbType.Int, 10, "User_ID");
        sqlCommand.Parameters.Add("@Inv_UserRevice", SqlDbType.NVarChar, 100, "Inv_UserRevice");
        sqlCommand.Parameters.Add("@Inv_UserRevicePhone", SqlDbType.VarChar, 50, "Inv_UserRevicePhone");
        sqlCommand.Parameters.Add("@Inv_UserReviceAddress", SqlDbType.NVarChar, 150, "Inv_UserReviceAddress");
        sqlCommand.Parameters.Add("@Inv_PayType", SqlDbType.Int, 10, "Inv_PayType");
        sqlCommand.Parameters.Add("@Inv_DateRevice", SqlDbType.DateTime, 50, "Inv_DateRevice");
        sqlCommand.Parameters.Add("@Local_ID", SqlDbType.Int, 10, "Local_ID");
        sqlCommand.Parameters.Add("@Inv_Note", SqlDbType.NVarChar, 150, "Inv_Note");
        sqlCommand.Parameters.Add("@Inv_UserReviceEmail", SqlDbType.VarChar, 50, "Inv_UserReviceEmail");


        sqlAdapter.InsertCommand = sqlCommand;
        sqlAdapter.Update(datatable);
        sqlCommand.Dispose();
        sqlCommand = null;
    }
    public static DataTable Get_InvetoryAsTable(string whereSql, string orderBy)
    {
        DataTable dt =  DataProvider.GetDataTable("select * from Invetory "+ whereSql +" "+ orderBy );
        //WebMsgBox.Show(dt.Rows.Count.ToString());
        return dt;
    }
    public static Invetory Get_Invetory_ByID(System.String Inv_ID)
    {
        DataTable dt = DataProvider.GetDataTable("select * from Invetory where Inv_ID = '" + Inv_ID +"'");
        Invetory temp = new Invetory();
        temp.Inv_ID = (dt.Rows[0]["Inv_ID"] is DBNull) ? string.Empty : (System.String)dt.Rows[0]["Inv_ID"];
        temp.Inv_CreateDate = (dt.Rows[0]["Inv_CreateDate"] is DBNull) ? DateTime.MinValue : (System.DateTime)dt.Rows[0]["Inv_CreateDate"];
        temp.Inv_TotalPrice = (dt.Rows[0]["Inv_TotalPrice"] is DBNull) ? int.MinValue : (System.Double)dt.Rows[0]["Inv_TotalPrice"];
        temp.Inv_TotalQuantity = (dt.Rows[0]["Inv_TotalQuantity"] is DBNull) ? int.MinValue : (System.Int32)dt.Rows[0]["Inv_TotalQuantity"];
        temp.Int_Status = (dt.Rows[0]["Int_Status"] is DBNull) ? int.MinValue : (System.Int32)dt.Rows[0]["Int_Status"];
        temp.User_ID = (dt.Rows[0]["User_ID"] is DBNull) ? int.MinValue : (System.Int32)dt.Rows[0]["User_ID"];
        temp.Inv_UserRevice = (dt.Rows[0]["Inv_UserRevice"] is DBNull) ? string.Empty : (System.String)dt.Rows[0]["Inv_UserRevice"];
        temp.Inv_UserRevicePhone = (dt.Rows[0]["Inv_UserRevicePhone"] is DBNull) ? string.Empty : (System.String)dt.Rows[0]["Inv_UserRevicePhone"];
        temp.Inv_UserReviceAddress = (dt.Rows[0]["Inv_UserReviceAddress"] is DBNull) ? string.Empty : (System.String)dt.Rows[0]["Inv_UserReviceAddress"];
        temp.Inv_PayType = (dt.Rows[0]["Inv_PayType"] is DBNull) ? int.MinValue : (System.Int32)dt.Rows[0]["Inv_PayType"];
        temp.Inv_DateRevice = (dt.Rows[0]["Inv_DateRevice"] is DBNull) ? DateTime.MinValue : (System.DateTime)dt.Rows[0]["Inv_DateRevice"];
        temp.Local_ID = (dt.Rows[0]["Local_ID"] is DBNull) ? int.MinValue : (System.Int32)dt.Rows[0]["Local_ID"];
        temp.Inv_Note = (dt.Rows[0]["Inv_Note"] is DBNull) ? string.Empty : (System.String)dt.Rows[0]["Inv_Note"];
        temp.Inv_UserReviceEmail = (dt.Rows[0]["Inv_UserReviceEmail"] is DBNull) ? string.Empty : (System.String)dt.Rows[0]["Inv_UserReviceEmail"];
        return temp;
    }
    public static void Delete_Invetory(Invetory temp)// delte hem dc nek
    {
        try
        {
            String strSQL;
            // xoa comment co pro lien quan
            strSQL = "delete  from Basket where Inv_ID = '" + temp.Inv_ID + " ' ";
            sqlConnection = new SqlConnection();
            sqlConnection = DataProvider.CreateConnection();
            SqlCommand cmd = new SqlCommand(strSQL, sqlConnection);
            cmd.CommandType = CommandType.Text; ;
            cmd.ExecuteNonQuery();

            // xoa Basket co Pro lien quan
            strSQL = "delete  from Invetory where Inv_ID = '" + temp.Inv_ID + " ' ";
            cmd = new SqlCommand(strSQL, sqlConnection);
            cmd.CommandType = CommandType.Text; ;
            cmd.ExecuteNonQuery();


            sqlConnection.Close();
            cmd = null;
        }
        catch (Exception e) { WebMsgBox.Show("ngoai le produc"); }
    }


}
